package com.wing.warehouse.controller.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.platform.order.model.response.WarehouseVO;
import com.wing.product.model.response.WarehouseProductInventoryVO;
import com.wing.product.service.WarehouseProductInventoryService;
import com.wing.warehouse.dao.WarehouseFactoryAdministrationDao;
import com.wing.warehouse.dao.WarehouseFactoryShelfSpaceDao;
import com.wing.warehouse.model.entity.WarehouseFactoryAdministrationVO;
import com.wing.warehouse.model.entity.WarehouseFactoryShelfSpaceEntity;
import com.wing.warehouse.model.entity.WarehouseFactoryShelfSpaceVO;
import com.wing.warehouse.model.entity.WrehouseFactoryAdministrationEntity;
import com.wing.warehouse.model.request.WarehouseFactoryAdministrationForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WarehouseFactoryAdministrationServiceImpl extends ServiceImpl<WarehouseFactoryAdministrationDao, WrehouseFactoryAdministrationEntity> implements WarehouseFactoryAdministrationService {

    @Autowired
    private WarehouseProductInventoryService warehouseProductInventoryService;
    @Autowired
    private WarehouseFactoryShelfSpaceService warehouseFactoryShelfSpaceService;
    @Autowired
    WarehouseFactoryShelfSpaceDao warehouseFactoryShelfSpaceDao;

    @Value("${platform.gatewayUrl}")
    private String gatewayUrl;
    @Override
    public int add(WarehouseFactoryAdministrationForm warehouseForm) {
        //校验
        WrehouseFactoryAdministrationEntity warehouse =new WrehouseFactoryAdministrationEntity();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WrehouseFactoryAdministrationEntity.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseForm, warehouse, copyOptions);
        return baseMapper.insert(warehouse);
    }

    @Override
    public List<WarehouseFactoryAdministrationVO> selectAll(String productId) {
        List<WarehouseFactoryAdministrationVO> warehouseFactoryAdministrationVOList = new ArrayList<>();
        List<WarehouseFactoryAdministrationVO> wrehouseFactoryAdministrationEntities = this.baseMapper.selectAllList();

//        List<Long> ids = wrehouseFactoryAdministrationEntities.stream().map(WarehouseFactoryAdministrationVO::getId).collect(Collectors.toList());
       //统计库存
//        List<WarehouseProductInventoryVO> warehouseProductInventoryVos = warehouseProductInventoryService.selectCount(ids,productId);
//        List<WarehouseProductInventoryVO> warehouseProductInventoryVOList = warehouseProductInventoryService.selectInventory(ids, productId);
//        Map<Long, Integer> inventoryVoMap = warehouseProductInventoryVos.stream().collect(Collectors.toMap(WarehouseProductInventoryVO::getWarehouseId, WarehouseProductInventoryVO::getInventorySum));
//        List<Long> warehouseShelfSpaceIdList = warehouseProductInventoryVOList.stream().map(WarehouseProductInventoryVO::getWarehouseShelfSpaceId).collect(Collectors.toList());
//        List<WarehouseFactoryShelfSpaceVO> warehouseFactoryShelfSpaceVOList = warehouseFactoryShelfSpaceService.selectFactoryCount(ids);

        List<WarehouseFactoryShelfSpaceVO> warehouseFactoryShelfSpaceVOList = new ArrayList<>();
//        if(CollectionUtils.isNotEmpty(warehouseShelfSpaceIdList)){
//            warehouseFactoryShelfSpaceVOList = warehouseFactoryShelfSpaceService.selectFactoryCount(warehouseShelfSpaceIdList);

//        }
        warehouseFactoryShelfSpaceVOList = warehouseFactoryShelfSpaceDao.selectFactoryList();
            for (WarehouseFactoryAdministrationVO we : wrehouseFactoryAdministrationEntities){
                List<WarehouseFactoryShelfSpaceVO> warehouseFactoryShelfSpaceVOS = new ArrayList<>();
//                if (!inventoryVoMap.isEmpty() && inventoryVoMap.containsKey(we.getId())){
//                    we.setInventorySum(inventoryVoMap.get(we.getId()));
//                }
                if (CollectionUtils.isNotEmpty(warehouseFactoryShelfSpaceVOList)){
                    for (WarehouseFactoryShelfSpaceVO ws : warehouseFactoryShelfSpaceVOList){
                        if (ws.getWarehouseId().equals(we.getId())){
                            warehouseFactoryShelfSpaceVOS.add(ws);
//                        warehouse.getWarehouseFactoryShelfSpaceVOList().add(ws);
                        }
                    }
                    we.setWarehouseFactoryShelfSpaceVOList(warehouseFactoryShelfSpaceVOS);
                }

                warehouseFactoryAdministrationVOList.add(we);
            }

        return warehouseFactoryAdministrationVOList;
    }

    @Override
    public List<WarehouseFactoryAdministrationVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<WarehouseFactoryAdministrationVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WarehouseFactoryAdministrationVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public WarehouseFactoryAdministrationVO findFactoryInfo(Long id) {
        return baseMapper.findFactoryInfo(id);
    }

    @Override
    public List<WarehouseFactoryShelfSpaceVO> findFactoryShelfSpace(String warehouseId) {

        return baseMapper.findFactoryShelfSpaceByWarehouseId(warehouseId);
    }

    @Override
    public List<WarehouseFactoryAdministrationVO> findFactoryShelfSpaceByWarehouseAreaId(Long warehouseAreaId) {
        return baseMapper.findFactoryShelfSpaceByWarehouseAreaId(warehouseAreaId);
    }

    @Override
    public WarehouseFactoryShelfSpaceVO findFactoryShelfByShelId(Long ShelId) {
        return baseMapper.findFactoryShelfByShelId(ShelId);
    }

    @Override
    public List<WarehouseFactoryShelfSpaceVO> info(Long id) {
        return baseMapper.info(id);
    }

    @Override
    public List<WrehouseFactoryAdministrationEntity> findByContract(Long id) {
        return baseMapper.findByContract(id);
    }
}
